<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>用户基本信息</span>
          </div>
          <el-form ref="form" :model="userInfo" label-width="auto">
            <el-form-item label="账号">
              <el-input v-model="userInfo.username" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="昵称">
              <el-input v-model="userInfo.nickName" disabled></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="userInfo.email" disabled></el-input>
            </el-form-item>
            <el-form-item label="创建时间">
              <el-input v-model="userInfo.createTime" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="key">
              <el-input v-model="userInfo.key" type="password" show-password></el-input>
            </el-form-item>
            <el-form-item>
              <el-button size="small" type="success" @click="submitForm">保存key</el-button>
              <el-button size="small" type="success" @click="getInfoByKey">通过key同步信息</el-button>
              <el-button size="small" type="success" @click="getKey">获取key</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {userPostRequire} from '@/api/user'

export default {
  name: 'home',
  data() {
    return {
      userInfo: {}
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      userPostRequire('getUser', {}).then(response => {
        if (response) {
          this.userInfo = response.data || {}
        }
      })
    },
    submitForm() {
      userPostRequire('update', this.userInfo).then(response => {
        if (response) {
          this.$message({
            message: response.message,
            type: response.status ? 'success' : 'error'
          })
        }
      }).finally(() => {
        window.location.reload();
      })
    },
    getInfoByKey() {
      userPostRequire('getInfoByKey', {key: this.userInfo.key}).then(response => {
        if (response) {
          this.$message({
            message: response.message,
            type: response.status ? 'success' : 'error'
          })
        }
      }).finally(() => {
        window.location.reload();
      })
    },
    getKey() {
      window.open('https://www.baidu.com');
    }
  }
}
</script>

<style scoped>
.app-container {
  margin-top: 40px;
  margin-left: 120px;
  margin-right: 120px;
}

</style>
